package com.qf.java2105.huangchuang.hotel.controller.food;

import com.alibaba.druid.util.StringUtils;
import com.qf.java2105.huangchuang.hotel.entity.ResultVO;
import com.qf.java2105.huangchuang.hotel.factory.BeanFactory;
import com.qf.java2105.huangchuang.hotel.service.IFoodService;
import com.qf.java2105.huangchuang.hotel.service.impl.FoodServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 菜品删除控制
 * @author hcvg
 * @version v1.0
 * @date 2021/09/12
 **/
@WebServlet("/food/delete")
public class FoodDeleteController extends HttpServlet {
    IFoodService foodService = (IFoodService) BeanFactory.getBean("foodService");

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResultVO<String> resultVO = null;
        try {
            String idStr = request.getParameter("id");
            if (!StringUtils.isEmpty(idStr)){
                resultVO = foodService.deleteById(Integer.valueOf(idStr));
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            response.getWriter().write("<script>alert(" + resultVO.getMessage() + ");</script>");
        }
        // 删除后
        response.sendRedirect(request.getContextPath() + "/food/search");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
